package com.uzai.console.web.fd;

import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.common.dto.fadan.FollowQfMsgDto;
import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.entity.*;
import com.uzai.console.mapper.*;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.fd.FdFollowSendSyncService;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 发单自动跟发发送
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "气氛组自动跟发发单消息",tags = "个人微信|发单|气氛组自动跟发发单消息")
@ApiSort(105)
@RestController
public class FdFollowQfSendController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(FdFollowQfSendController.class);

    @Autowired
    private FdFollowSendSyncService fdFollowSendSyncService;
    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private DeviceWechatInfoMapper deviceWechatInfoMapper;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private FdFollowConfigQfMapper fdFollowConfigQfMapper;
    @Autowired
    private FdFollowConfigQfKeywordMapper fdFollowConfigQfKeywordMapper;
    @Autowired
    private FdFollowConfigQfGroupTuoMapper fdFollowConfigQfGroupTuoMapper;


    @ApiOperation(value = "气氛组跟发发单消息", notes = "气氛组跟发发单消息")
    @ApiOperationSupport(order = 1)
    @PostMapping("/fd/followQfSend")
    public Object followQfSend(@RequestBody FollowQfMsgDto followQfMsgDto){

        logger.info("--气氛组跟发发送内容--followQfMsgDto={}", JSONObject.toJSONString(followQfMsgDto));

        if(followQfMsgDto.getMerId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "运营商id不能为空");
        }

        if(followQfMsgDto.getFdFollowConfigQfId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该气氛组跟发所属的配置任务id不能为空");
        }

        FdFollowConfigQf fdFollowConfigQf = fdFollowConfigQfMapper.selectById(followQfMsgDto.getFdFollowConfigQfId(), followQfMsgDto.getMerId());
        if(fdFollowConfigQf == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该气氛组配置任务id对于的任务不存在或者已经删除");
        }

        Merchant merchant = merchantMapper.selectById(followQfMsgDto.getMerId());
        if(merchant == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "运营商不存在");
        }

        if(followQfMsgDto.getDeviceUniqueId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "监控机器人唯一id不能为空");
        }

        if(followQfMsgDto.getDeviceType() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "监控机器人类型不能为空");
        }

        DeviceTypeEnum deviceTypeEnum = DeviceTypeEnum.getById(followQfMsgDto.getDeviceType());
        if(deviceTypeEnum == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "监控机器类型不存在");
        }

        Device device = deviceService.findDeviceInfoByDeviceUniqueId(followQfMsgDto.getDeviceUniqueId(), deviceTypeEnum.getKey(), followQfMsgDto.getMerId());
        if(device == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "监控机器人不存在");
        }

        if(StringUtils.isBlank(followQfMsgDto.getGroupWxid())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "监控群wxid为空");
        }

        if(StringUtils.isBlank(followQfMsgDto.getSendWxid()) ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "被监控群成员wxid不能为空");
        }

        if(StringUtils.isBlank(followQfMsgDto.getKeyword()) ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "匹配关键词不能为空");
        }

        //查询匹配关键词
        FdFollowConfigQfKeyword fdFollowConfigQfKeyword = fdFollowConfigQfKeywordMapper.selectByUniqueIndex(followQfMsgDto.getMerId(), followQfMsgDto.getFdFollowConfigQfId(), followQfMsgDto.getKeyword());
        if(fdFollowConfigQfKeyword == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该配置任务未找到该关键词");
        }

        //查找该群托信息
        FdFollowConfigQfGroupTuo fdFollowConfigQfGroupTuo = fdFollowConfigQfGroupTuoMapper.selectByUniqueIndex(followQfMsgDto.getMerId(), followQfMsgDto.getGroupWxid(), followQfMsgDto.getSendWxid(), device.getDeviceId());
        if(fdFollowConfigQfGroupTuo == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该监控机器人未配置气氛组自动跟发任务");
        }

        fdFollowSendSyncService.followQfSendSync(followQfMsgDto);

        return true;
    }


}
